import {_decorator, Component, Label, resources, Sprite, SpriteFrame, Node} from "cc";
import {FreshImmortal} from "db://assets/proto/shared/v1/wuni";
import {serverTimeHelper} from "db://assets/script/server-time-helper";
import {configsHelper} from "db://assets/script/configs-helper";
import {TalentHeadComp} from "db://assets/script/game/mainui/talent-head-comp";
import {lobby} from "db://assets/script/lobby";
import {FishInfoComp} from "db://assets/script/game/fish/fish-info-comp";

const {ccclass, property} = _decorator;

@ccclass
export default class PoolRankItem extends Component {

    @property(TalentHeadComp)
    head!: TalentHeadComp;

    @property(Label)
    nameLabel!: Label;

    @property(Label)
    ageLabel!: Label;

    @property(Sprite)
    realmShow!: Sprite;

    @property(Label)
    idxLabel!: Label;

    fish!: FreshImmortal;
    rank!: number;

    init(rank: number, fish: FreshImmortal) {
        this.fish = fish;
        this.rank = rank;
        this.UpdateUI()
    }


    UpdateUI() {
        this.head.setHead(this.fish.id, this.fish.baseInfo!.gender, this.fish.baseInfo!.talent, this.fish.masterInfo!.masterId.toString())
        this.nameLabel.string = this.fish.baseInfo!.fishName;
        this.ageLabel.string = '「' + (Math.ceil((serverTimeHelper.loadServerTime() - (this.fish.baseInfo!.birthAtMs.toNumber())) / 1000 / 60 / 60 / 2)) + "岁" + '」'
        const realmShowIconPath = configsHelper.loadRealmIcon(this.fish.assetInfo!.level)!
        this.realmShow.spriteFrame = resources.get(realmShowIconPath, SpriteFrame)!
        this.idxLabel.string = this.rank.toString();
        this.head.node.on(Node.EventType.TOUCH_END, () => {
            const fishInfoUI = lobby.instance.addUI("prefab/fish-info")
            fishInfoUI.getComponent(FishInfoComp)!.changeFishDetail(this.fish)
        })
    }

    protected onDestroy(): void {
    }

    update(deltaTime: number) {

    }

}
